﻿#include "WhiteFoxman.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    WhiteFoxman::WhiteFoxman(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool WhiteFoxman::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void WhiteFoxman::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (getEnvir()->Random->Next(8) != 0)
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            tempVar2->Type = 1;
            Broadcast(tempVar2);

            //TODO - Change to PoisonTarget with equation
            if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= getTarget()->Stats[Stat::PoisonResist])
            {
                int levelgap = 50 - getTarget()->getLevel();
                if (getEnvir()->Random->Next(20) < 4 + levelgap)
                {
                    Poison *tempVar3 = new Poison(), *this;
                    tempVar3->Owner = this;
                    tempVar3->Duration = 5;
                    tempVar3->PType = PoisonType::Slow;
                    tempVar3->TickSpeed = 1000;
                    getTarget()->ApplyPoison(tempVar3, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                }
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void WhiteFoxman::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && (getEnvir()->getTime() < FearTime))
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 5000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist >= AttackRange)
        {
            MoveTo(getTarget()->getCurrentLocation());
        }
        else
        {
            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }

        }
    }
}
